package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbProductAchievement;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbProductLowCarbonCertBo;
import org.dromara.system.domain.vo.WbProductLowCarbonCertVo;
import org.dromara.system.domain.WbProductLowCarbonCert;
import org.dromara.system.mapper.WbProductLowCarbonCertMapper;
import org.dromara.system.service.IWbProductLowCarbonCertService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品绿色低碳证书Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-25
 */
@RequiredArgsConstructor
@Service
public class WbProductLowCarbonCertServiceImpl implements IWbProductLowCarbonCertService {

    private final WbProductLowCarbonCertMapper baseMapper;

    /**
     * 查询产品绿色低碳证书
     */
    @Override
    public WbProductLowCarbonCertVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品绿色低碳证书列表
     */
    @Override
    public TableDataInfo<WbProductLowCarbonCertVo> queryPageList(WbProductLowCarbonCertBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbProductLowCarbonCert> lqw = buildQueryWrapper(bo);
        Page<WbProductLowCarbonCertVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品绿色低碳证书列表
     */
    @Override
    public List<WbProductLowCarbonCertVo> queryList(WbProductLowCarbonCertBo bo) {
        LambdaQueryWrapper<WbProductLowCarbonCert> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbProductLowCarbonCert> buildQueryWrapper(WbProductLowCarbonCertBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbProductLowCarbonCert> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getCompanyName() != null, WbProductLowCarbonCert::getCompanyName, bo.getCompanyName());
        lqw.eq(bo.getCompanyId() != null, WbProductLowCarbonCert::getCompanyId, bo.getCompanyId());
        lqw.like(bo.getProductCategoryName() != null, WbProductLowCarbonCert::getProductCategoryName, bo.getProductCategoryName());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), WbProductLowCarbonCert::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getProductModel()), WbProductLowCarbonCert::getProductModel, bo.getProductModel());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluationAgency()), WbProductLowCarbonCert::getEvaluationAgency, bo.getEvaluationAgency());
        lqw.eq(StringUtils.isNotBlank(bo.getGreenhouseGasEmissionsPerFunctionalUnit()), WbProductLowCarbonCert::getGreenhouseGasEmissionsPerFunctionalUnit, bo.getGreenhouseGasEmissionsPerFunctionalUnit());
        lqw.eq(bo.getIssueTime() != null, WbProductLowCarbonCert::getIssueTime, bo.getIssueTime());
        lqw.eq(bo.getEndTime() != null, WbProductLowCarbonCert::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSupportingMaterialUrl()), WbProductLowCarbonCert::getSupportingMaterialUrl, bo.getSupportingMaterialUrl());
        lqw.orderByDesc(WbProductLowCarbonCert::getId);
        return lqw;
    }

    /**
     * 新增产品绿色低碳证书
     */
    @Override
    public Boolean insertByBo(WbProductLowCarbonCertBo bo) {
        WbProductLowCarbonCert add = MapstructUtils.convert(bo, WbProductLowCarbonCert.class);
        validEntityBeforeSave(add);
        Long companyId = add.getCompanyId();
        String companyName=baseMapper.getCompanyName(companyId);
        add.setCompanyName(companyName);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品绿色低碳证书
     */
    @Override
    public Boolean updateByBo(WbProductLowCarbonCertBo bo) {
        WbProductLowCarbonCert update = MapstructUtils.convert(bo, WbProductLowCarbonCert.class);
        validEntityBeforeSave(update);
        Long companyId = update.getCompanyId();
        String companyName=baseMapper.getCompanyName(companyId);
        update.setCompanyName(companyName);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbProductLowCarbonCert entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品绿色低碳证书
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
